﻿using System.Diagnostics;
using Quartz;

namespace Admin.ApplicationLib.Jobs
{
    public class LocalLoadJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            var jobDetailJobDataMap = context.JobDetail.JobDataMap;
            var processPath = jobDetailJobDataMap.Get("ProcessPath")?.ToString();

            if (string.IsNullOrEmpty(processPath))
            {
                throw new ArgumentNullException("");
            }

            var processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = processPath;
            try
            {
                // Start the process with the info we specified.
                // Call WaitForExit and then the using statement will close.
                using (Process? exeProcess = Process.Start(processStartInfo))
                {
                    exeProcess?.WaitForExit();
                }
            }
            catch
            {
                // Log error.
            }

            return Task.CompletedTask;
        }
    }
}